package com.dhsc.service.impl.ywzj.kbpzgl.pzbdlslr;

import com.dhsc.domain.ywzx.kbpzgl.PzbdlslrVo;
import com.dhsc.domain.ywzx.kbpzgl.WdlysqlrVo;
import com.dhsc.domain.ywzx.kbpzgl.WdpzscwjVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.ywzj.kbpzgl.pzbdlslr.IPzbdlslrDao;
import com.dhsc.service.ywzj.pzbdlslr.IPzbdlslrService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Slf4j
@Service
public class PzbdlslrServiceImpl implements IPzbdlslrService {

    @Autowired
    private IPzbdlslrDao pzbdlslrDao;


    @Override
    public LayuiTableVo queryWdpzrkinfo(PzbdlslrVo p) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(p.getPage(), p.getLimit());
            List<PzbdlslrVo> list;
            if(p.getZt() == 1){
                list= pzbdlslrDao.queryWdpzrkinfo(p);
            }else{
                list= pzbdlslrDao.queryWdpzrkinfos(p);
            }
            PageInfo<PzbdlslrVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询凭证变动流水信息出错",e);
            throw new MyException("查询凭证变动流水信息出错",e);
        }
        return obj;
    }

    @Override
    @Transactional("localTransactionManager")
    public ResponseObject saveLsd(PzbdlslrVo p) {
        ResponseObject obj = new ResponseObject();
        try {
            //查询申请编号最新的申请编号
            PzbdlslrVo pz = pzbdlslrDao.queryNewSqdbh(p);
            p.setSqbh(pz.getSqbh());
            pzbdlslrDao.saveLsd(p);
            pzbdlslrDao.saveLsdLsmx(p);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("保存流水单信息出错",e);
            throw new MyException("保存流水单信息出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryPzlsmx(PzbdlslrVo p) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            ArrayList<WdlysqlrVo> objects = new ArrayList<>();
            List<WdlysqlrVo> list = pzbdlslrDao.queryPzlsmx(p);
            //查询凭证类型字典
            WdlysqlrVo w = new WdlysqlrVo();
            w.setSqbh(p.getSqbh());
            List<WdlysqlrVo> listzd = pzbdlslrDao.queryWdrkPzlxTxtdm(w);
            List<PzbdlslrVo>  listfb = pzbdlslrDao.queryFbyybxx(p);
            LinkedHashMap<String, WdlysqlrVo> uniqueData = new LinkedHashMap<>();
            for (WdlysqlrVo entry  : listzd) {
                if (!uniqueData.containsKey(entry.getPzlx())) {
                    uniqueData.put(entry.getPzlx(), entry);
                }
            }
            List<WdlysqlrVo> uniqueDataList = new ArrayList<>(uniqueData.values());
            ArrayList<String> arrayList = new ArrayList<>();
            for (int i = 0; i < listfb.size(); i++) {
                arrayList.add(listfb.get(i).getBmmc());
            }
            for (WdlysqlrVo wdlysqlrVo1 : uniqueDataList) {
                wdlysqlrVo1.setSqsl(0);
                wdlysqlrVo1.setPid("-1");
                wdlysqlrVo1.setZt("1");
                wdlysqlrVo1.setId(wdlysqlrVo1.getBm());
                wdlysqlrVo1.setPzbm(wdlysqlrVo1.getBmsm());
            }
            for (WdlysqlrVo wdlysqlrVo : list) {
                wdlysqlrVo.setPid(wdlysqlrVo.getPzlx());
                wdlysqlrVo.setId(wdlysqlrVo.getPzbm());
                wdlysqlrVo.setPzbm(wdlysqlrVo.getPzbm());
                wdlysqlrVo.setDryyb(arrayList);
            }
            objects.addAll(list);
            objects.addAll(listzd);
            obj.setData(objects);
            obj.setCode(200);
            obj.setCount(objects.size());
            obj.setMsg("");
        }catch (Exception e){
            obj.setCode(500);
            throw new MyException("查询网点领用申请录入编辑信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject updateWdpzrkXx(PzbdlslrVo p) {
        ResponseObject obj = new ResponseObject();
        try {
            if(p.getNum() == 1){
                pzbdlslrDao.updateWdpzrkXxSqsl(p);
            }else if(p.getNum() == 2){
                pzbdlslrDao.updateWdpzrkXxBz(p);
            }else if(p.getNum() == 3){
                pzbdlslrDao.updateWdpzrkXxBz1(p);
            }else if(p.getNum() == 4){
                pzbdlslrDao.updateWdpzrkXxLKhh(p);
            }else if(p.getNum() == 5){
                pzbdlslrDao.updateWdpzrkXxDryyb(p);
            }else if(p.getNum() == 6){
                pzbdlslrDao.updateWdpzrkXxJdr(p);
            }
            obj.setStatus(200);
        }catch (Exception e){
            log.error("网点凭证入库修改数量凭证编号和备注出错",e);
            throw new MyException("网点凭证入库修改数量凭证编号和备注出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject saveWdpzrkLsd(PzbdlslrVo p) {
        ResponseObject obj = new ResponseObject();
        try {
            boolean flag = false;
            if(p.getNum() == 1){
                int a = pzbdlslrDao.querySftxbhfw(p);
                if(a > 0){
                    obj.setStatus(201);
                    obj.setMessage("提交失败,对于需要编号的凭证,请填写编号的范围!!");
                    return obj;
                }else{
                    flag = true;
                }
            }
            List<PzbdlslrVo> list = pzbdlslrDao.querySfbxBhbz(p);
            if(p.getShzt() == 1){
                //审核出库领用时做判断
                if(p.getLslx().equals("02")){
                    if(list.get(0).getBhbz().equals("1")){
                        p.setLszt("03");
                    }else{
                        p.setLszt("02");
                        pzbdlslrDao.callPROCPZZK();
                    }
                    pzbdlslrDao.updateWdpzrkZt(p);
                }else{
                    //审核时保存
                    p.setLszt("02");
                    pzbdlslrDao.updateWdpzrkZt(p);
                    //调用存储过程更新凭证使用情况:
                    pzbdlslrDao.callPROCPZZK();
                }
            }else{
                //根据申请编号查询有多少笔记录申请数量大于0
                int b = pzbdlslrDao.queryHjSqsl(p);
                if(b < 1){
                    obj.setStatus(201);
                    obj.setMessage("申请数量不能为空");
                    return obj;
                }else{
                    flag = true;
                }
                int g = 0;
                if(p.getLslx().equals("02")){
                    int count1 =  pzbdlslrDao.querySfbxpzbh(p);
                    int count2 =  pzbdlslrDao.querySfbxpzbhs(p);
                    if(count1 >0 && count2 > 0 || count1>1){
                        obj.setStatus(201);
                        obj.setMessage("依据监管要求，有编号凭证逐份使用并匹配客户号，因此一次只能出库一份；请撤回申请单重新提交");
                        return obj;
                    }
                }
                if(p.getLslx().equals("07")){
                    //出库调拨查询调拨营业部是否必填
                    List<PzbdlslrVo> dryyb = pzbdlslrDao.queryDryybxx(p);
                    String pzbm ="";
                    if(CommonUtil.isNotEmptyList(dryyb)){
                        for (int i = 0; i < dryyb.size(); i++) {
                            if(!CommonUtil.isNotEmptyObj(dryyb.get(i).getDryyb())){
                                flag = false;
                                pzbm = dryyb.get(i).getPzbm();
                                break;
                            }
                        }
                        if(!flag){
                            obj.setStatus(201);
                            obj.setMessage("提交失败,凭证编码："+pzbm+"请填写调入营业部!!");
                            return obj;
                        }
                    }else{
                        obj.setStatus(201);
                        obj.setMessage("提交失败,请填写调入营业部!!");
                        return obj;
                    }

                    List<PzbdlslrVo> jdr = pzbdlslrDao.queryJdrxx(p);
                    if(CommonUtil.isNotEmptyList(jdr)){
                        for (int i = 0; i < jdr.size(); i++) {
                            if(!CommonUtil.isNotEmptyObj(jdr.get(i).getJdr())){
                                pzbm = dryyb.get(i).getPzbm();
                                flag = false;
                                break;
                            }
                        }
                        if(!flag){
                            obj.setStatus(201);
                            obj.setMessage("提交失败,凭证编码："+pzbm+"请填写借调人!!");
                            return obj;
                        }
                    }else{
                        obj.setStatus(201);
                        obj.setMessage("提交失败,请填写借调人!!");
                        return obj;
                    }
                }

                if(p.getState().equals("1")){
                    //查询是否上传图片
                    List <PzbdlslrVo> listtp= pzbdlslrDao.querySfsctp(p);
                    //查询需要上传图片的数据
                    List <PzbdlslrVo> lists= pzbdlslrDao.querySfsctps(p);
                    if(listtp.size() == lists.size()){
                        int z= 0;
                        for (int i = 0; i < lists.size(); i++) {
                            if(!lists.get(i).getPzbh().equals(listtp.get(i).getPzbh())){
                                z++;
                                break;
                            }else{
                                z = 0;
                            }
                        }
                        if(z == 0){
                            flag = true;
                        }else{
                            obj.setStatus(201);
                            obj.setMessage("提交失败,请上传作废图片!!");
                            return obj;
                        }
                    }else{
                        obj.setStatus(201);
                        obj.setMessage("提交失败,请上传作废图片!!");
                        return obj;
                    }
                }
                if(flag){
                    p.setLszt("01");
                    pzbdlslrDao.updateWdpzrkZt(p);
                }
            }
            obj.setStatus(200);
        }catch (Exception e){
            log.error("网点凭证入库修改数量凭证编号和备注出错",e);
            throw new MyException("网点凭证入库修改数量凭证编号和备注出错",e);
        }
        return obj;
    }

    @Override
    @Transactional("localTransactionManager")
    public ResponseObject PzbdlsSclsd(PzbdlslrVo p) {
        ResponseObject obj = new ResponseObject();
        try {
            //查询是否需要填写编号范围
            pzbdlslrDao.deleteLsmx(p);
            pzbdlslrDao.deleteBdls(p);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("删除流水单出错",e);
            throw new MyException("删除流水单出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject PzbdlsThlsd(PzbdlslrVo p) {
        ResponseObject obj = new ResponseObject();
        try {
            pzbdlslrDao.updateThBdls(p);
            pzbdlslrDao.callPROCPZZK();
            obj.setStatus(200);
        }catch (Exception e){
            log.error("退回流水单出错",e);
            throw new MyException("退回流水单出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject WdpzScwj(WdpzscwjVo w) {
        ResponseObject obj = new ResponseObject();
        try {
            //查看当前文件是否存在
            int a = pzbdlslrDao.queryPzbdlsScwj(w);
            if(a > 0) {
                //已经上传过文件的 重新上传时修改文件名称
                pzbdlslrDao.updateWdpzrkScwjmc(w);
            }else{
                pzbdlslrDao.WdpzScwj(w);
            }
            obj.setStatus(200);
        }catch (Exception e){
            log.error("保存上传文件信息出错",e);
            throw new MyException("保存上传文件信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject savezftj(PzbdlslrVo p) {
        ResponseObject obj = new ResponseObject();
        try {
            //查询是否上传图片
            List <PzbdlslrVo> list= pzbdlslrDao.querySfsctp(p);
            //查询需要上传图片的数据
            List <PzbdlslrVo> lists= pzbdlslrDao.querySfsctps(p);
            if(list.size() == lists.size()){
                int z= 0;
                for (int i = 0; i < lists.size(); i++) {
                    if(!lists.get(i).getPzbh().equals(list.get(i).getPzbh())){
                        z++;
                        break;
                    }else{
                        z = 0;
                    }
                }
                if(z == 0){
                    p.setLszt("04");
                    pzbdlslrDao.updateWdpzrkZt(p);
                    pzbdlslrDao.callPROCPZZK();
                }else{
                    obj.setStatus(201);
                    obj.setMessage("提交失败,请上传作废图片!!");
                    return obj;
                }
            }else{
                obj.setStatus(201);
                obj.setMessage("提交失败,请上传作废图片!!");
                return obj;
            }
            obj.setStatus(200);
        }catch (Exception e){
            log.error("作废提交出错",e);
            throw new MyException("作废提交出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject saveyltj(PzbdlslrVo p) {
        ResponseObject obj = new ResponseObject();
        try {
            //查询是否录入客户号
            int a = pzbdlslrDao.querySflrKhh(p);
            if(a < 1) {
                p.setLszt("02");
                pzbdlslrDao.updateWdpzrkZt(p);
                pzbdlslrDao.callPROCPZZK();
            }else{
                obj.setStatus(201);
                obj.setMessage("提交失败,请填写客户号!!");
                return obj;
            }
            obj.setStatus(200);
        }catch (Exception e){
            log.error("已领提交出错",e);
            throw new MyException("已领提交出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject deleteScwj(PzbdlslrVo p) {
        ResponseObject obj = new ResponseObject();
        try {
            pzbdlslrDao.deleteScwj(p);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("删除上传文件出错",e);
            throw new MyException("删除上传文件出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryYwzxGsjgxx(PzbdlslrVo p) {
        ResponseObject obj = new ResponseObject();
        try {
            List<PzbdlslrVo> list = pzbdlslrDao.queryYwzxGsjgxx(p);
            obj.setData(list);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("查询业务中心网点凭证营业部信息出错",e);
            throw new MyException("查询业务中心网点凭证营业部信息出错",e);
        }
        return obj;
    }

}
